package net.wyy.ssocore;

import org.apache.commons.lang3.ArrayUtils;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Optional;
import java.util.stream.Stream;

/**
 * Created by Enzo Cotter on 2019/11/22.
 */
public class ServletUtils {
    public static void printJsessionId(HttpServletRequest request, boolean createSession) {
        boolean hasSessionId = false;
        if(ArrayUtils.isNotEmpty(request.getCookies())){
            Optional<Cookie> jsessionid = Stream.of(request.getCookies())
                    .filter(co -> co.getName().equalsIgnoreCase("jsessionid"))
                    .findFirst();
            if(jsessionid.isPresent()){
                System.out.println("jsessionid:"+jsessionid.get().getValue());
                hasSessionId = true;
            }else{
                System.out.println("jsessionid: 不存在！");
            }
        }else{
            System.out.println("No Cookies");
        }
        if(!hasSessionId && createSession){
            request.getSession();
            printJsessionId(request,false);
        }
    }
}
